<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\Cultivate;
use think\Db;
use think\Exception;

class CultivateManager extends BaseManager
{

    public static function apply($user,$data,$trans = true)
    {
        if ($trans){
            Db::startTrans();
        }
        try {
            $start = strtotime(date('Y-m-d 00:00:00'));
            $end = strtotime(date('Y-m-d 23:59:59'));
            $captainInfo = Cultivate::where('user_id',$user->id)->where('type',$data['type'])->where('create_time','between time',[$start,$end])->find();
            if (isset($captainInfo)){
                throw new Exception('您今日已申请当前类型培训，请耐心等待工作人员联系');
            }
            $data['user_id'] = $user->id;
            $data['create_time'] = time();
            $data['update_time'] = time();
            Cultivate::create($data);
            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }
}